<?php

// $action est deja set par l'appellant

$limit = request::get('nbritems');
if (!$limit) {
	$limit = 4;
}


/**
 *	Action INDEX
 *
 */

if ($action == "index") {

	$timestamp = request::get('timestamp');
	if (!$timestamp) {
		$timestamp = 0;
	}	
	$q = "SELECT * FROM messages WHERE timestamp > $timestamp ORDER BY timestamp DESC LIMIT $limit";
	
	sql::query($q);
	$data = sql::allFetchArray();
	echo(json_encode($data));
}


/**
 *	Action add
 *
 */
 

if ($action == "add") {

	$new = $_REQUEST;
	$data = array();
	$data['pseudo'] = request::get('pseudo');
	$data['message'] = request::get('message');
	$data['timestamp'] = time();	

	OrmNode::upsert("messages", array_keys($data), $data);	
	echo(json_encode($data));
}


